package com.cqgc.controller;

import com.cqgc.domain.NewPassword;
import com.cqgc.domain.PageHelper;
import com.cqgc.domain.Result;
import com.cqgc.domain.User;
import com.cqgc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("users")
public class UserController {

    @Autowired
    private UserService service;


    @PostMapping("login")
    public Result login(@RequestBody User user){

        Result result = new Result();
        User login = service.login(user);
        if (login != null){

            login.setRegTime(new Date());
            int i = service.UpdateTime(login);
            if (i>0){
                result.setCode(1);
                result.setMsg("登录成功");
                List<User> list=new ArrayList<>();
                list.add(login);
                result.setData(list);
            }else {
                result.setCode(0);
                result.setMsg("登录失败");
            }

//            result.setCode(1);
//            result.setMsg("登录成功");
//            List<User> list=new ArrayList<>();
//            list.add(login);
//            result.setData(list);
        }else {
            result.setCode(0);
            result.setMsg("用户名或密码错误");
        }

        return result;
    }

    @PostMapping("add")
    public Result AddUser(@RequestBody User user){

        Result result=new Result();
        if (user != null){
            user.setRegTime(new Date());
            String substring = user.getAccount().substring(5, 11);

            user.setPassword(substring);
            user.setLastLoginTime(new Date());
            user.setName(user.getAccount());
        }

        int i = service.addUser(user);

        if (i>0){
            result.setCode(1);
            result.setMsg("注册成功");
        }else {
            result.setCode(0);
            result.setMsg("注册失败");
        }


        return result;
    }

    @GetMapping("list")
    public Result findAll(PageHelper pageHelper){

        Result result = new Result();

        if (pageHelper != null){
            int page=(pageHelper.getCurrentPage()-1)*pageHelper.getPageSize();
            pageHelper.setCurrentPage(page);
        }
        List<User> all = service.findAll(pageHelper);
        List<User> count=service.findAllUser();
        if (all!=null){
             result.setCode(1);
             result.setMsg("查询成功");
             result.setData(all);
             result.setTotal(count.size());
        }else {
            result.setCode(0);
            result.setMsg("查询失败");
        }

        return result;
    }

    @PostMapping("edit")
    public Result editUser(@RequestBody User user){

        Result result = new Result();
        User userByUid = service.findUserByUid(user.getUid());

        if (user != null){
            if(user.getAccount()==null){
                user.setAccount(userByUid.getAccount());
            }
            if (user.getRole()==0){
                user.setRole(userByUid.getRole());
            }
        }else {
            result.setCode(0);
            result.setMsg("修改失败");
            return result;
        }

        int i = service.editUser(user);

        if (i>0){
            result.setCode(1);
            result.setMsg("修改成功");
        }else{
            result.setCode(0);
            result.setMsg("修改失败");
        }
        return result;
    }

    @PostMapping("editPassword")
    public Result editPassword(@RequestBody NewPassword newPassword){

        Result result = new Result();
        if (newPassword !=null){
            User userByUid = service.findUserByUid(newPassword.getUid());
            if (newPassword.getOldPassword().equals(userByUid.getPassword())){
                int i = service.editPassword(newPassword);
                if(i>0){
                    result.setCode(1);
                    result.setMsg("密码修改成功");
                    return result;
                }
            }
        }
        result.setCode(0);
        result.setMsg("密码修改失败");
        return result;

    }

    @PostMapping("del")
    public Result delUser(@RequestBody User user){
        Result result = new Result();
//        int i = service.delUser(user);
        int i = service.deleteUser(user);
        if (i>0){
            result.setCode(1);
            result.setMsg("删除成功");
        }else{
            result.setCode(0);
            result.setMsg("删除失败");
        }

        return result;
    }

}
